package at.jaki.ubb.crawling.demo.ui;

import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import at.jaki.ubb.crawling.demo.model.Point;
import at.jaki.ubb.crawling.demo.model.StarData;

public final class ChartUtils {

	private ChartUtils() {
	}

	public static JPanel generateChart(StarData byId) {
		JPanel p = new JPanel();

		XYSeries series1 = new XYSeries(byId.getId());
		List<Point> list = byId.getList();
		for (Point p2 : list) {
			if (p2.getY() != 0) {
				series1.add(p2.getX(), p2.getY() - byId.getAvg());
			}
		}

		XYSeriesCollection dataset2 = new XYSeriesCollection();
		dataset2.addSeries(series1);

		XYDotRenderer dot = new XYDotRenderer();

		dot.setDotHeight(3);
		dot.setDotWidth(3);

		NumberAxis xax = new NumberAxis("time");
		NumberAxis yax = new NumberAxis("luminosity");

		XYPlot plot = new XYPlot(dataset2, xax, yax, dot);
		JFreeChart chart2 = new JFreeChart(plot);

		ChartPanel chartPanel2 = new ChartPanel(chart2);
		p.add(chartPanel2);

		return p;
	}

}
